package dahuiji.findPassword.service;

import java.util.List;

import dahuiji.common.dao.BaceDao;
import dahuiji.common.entity.User;

public class FindPasswordService {
	private BaceDao baceDao;

	/**
	 * 找回密码，根据用户输入的邮箱检测是否存在
	 * 
	 * @param email
	 * @return
	 */
	public String getUserByEamil(String email) {
		if (email != null) {
			String sql = "SELECT u.EMAIL from tb_dahuiji_user u where u.EMAIL= "
					+ "'" + email + "'";
			baceDao = new BaceDao();
			List list = baceDao.executeQuerySql(sql);
			if (list.size() == 1) {
				//Object[] objects = (Object[]) list.get(0);
				String rString=(String) list.get(0);
				return rString;
			}
		}
		return null;
	}

	/**
	 * 找回密码时产生新密码
	 * 
	 * @return
	 */
	public String productPassword() {
		String reString = "";
		for (int i = 0; i < 6; i++) {
			reString = reString + (int) (Math.random() * 10);
		}
		System.out.println(reString);
		return reString;
	}
	/**
	 * 找回密码，系统给用户发送密码后，设置用户密码
	 * @param newPassword
	 * @param userId
	 * @return
	 */
	public boolean updatePassword(String newPassword, Long userId) {
		if (newPassword != null && userId != null) {
			String sqlString = "UPDATE tb_dahuiji_user set `PASSWORD` =" + "'"
					+ newPassword + "'" + "WHERE USER_ID=" + userId;
			baceDao = new BaceDao();
			baceDao.executeBySql(sqlString);
			return true;
		}

		return false;
	}

}
